<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<f:view xmlns="http://www.w3.org/1999/xhtml"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:p="http://primefaces.org/ui"
	xmlns:cc="http://java.sun.com/jsf/composite/customcomponents"
	contentType="text/html">
	
	<h:head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<title><h:outputText value="#{label['admin.logviewer.title']} - #{label['appNameTitle']}"/></title>
	</h:head>	
	<h:body>
		<cc:LatamHeader userInfo="true">
			<cc:AdminHeaderMenu/>
		</cc:LatamHeader>

		<p:growl id="growl" showDetail="false" showSummary="true" severity="INFO,WARN,ERROR"/>
		<p:panel styleClass="latam_body_panel">
			<p:fieldset legend="#{label['ExampleFunctions.logger.title']}">
				<h:form>			
					<p:panel styleClass="no-border">
						<p:panelGrid columns="3" styleClass="panelGridCenter"> 
							<p:outputLabel value="#{label['ExampleFunctions.logger.level']}:"/>
							<p:selectOneMenu id="levelName" value="#{exampleLoggerManager.levelName}">
								<f:selectItem itemLabel="TRACE" itemValue="TRACE"/>
								<f:selectItem itemLabel="DEBUG" itemValue="DEBUG"/>
								<f:selectItem itemLabel="INFO" itemValue="INFO"/>
								<f:selectItem itemLabel="WARN" itemValue="WARN"/>
								<f:selectItem itemLabel="ERROR" itemValue="ERROR"/>
								<f:selectItem itemLabel="OFF" itemValue="OFF"/>
							</p:selectOneMenu>
							<p:commandButton value="#{label['ExampleFunctions.logger.testLogger']}" 
								action="#{exampleLoggerManager.testLogger}" 
								update=":growl levelName"/>
						</p:panelGrid>
					</p:panel>
				</h:form>
			</p:fieldset>
			<br/>
				<p:fieldset legend="#{label['admin.logviewer.tail.title']}" toggleable="false" collapsed="false">
					<h:form id="viewer">
					<p:inputTextarea 
						id="applogger" widgetVar="applogger" 
						onkeydown="return false;" onkeyup="return false;" onkeypress="return false;"
						onmousedown="return false;" onmouseup="return false;" onclick="return false;"
						style="
							width: 915px; height: 270px; overflow-y: scroll; overflow-x: scroll;
							font-family: Courier, Monospace;
							color: black
							"/>
					<br/>
					<p:panel styleClass="no-border" style="text-align: center;">
						<p:commandButton value="#{label['admin.logviewer.tail.enable']}"
							action="#{exampleLogViewer.enableJmsLogger}" 
							rendered="#{!exampleLogViewer.jmsLoggerEnabled}"
							update="viewer"
							/>
						<p:commandButton value="#{label['admin.logviewer.tail.disable']}" 
							action="#{exampleLogViewer.disableJmsLogger}" 							
							rendered="#{exampleLogViewer.jmsLoggerEnabled}"
							update="viewer"
							/>
						<br/>
						<p:commandButton value="#{label['admin.logviewer.tail.test']}" 
							action="#{exampleLogViewer.testLogger}" 
							/>											
						<p:commandButton value="#{label['admin.logviewer.tail.clear']}" 
							onclick="clearViewer()"
							update="viewer"
							/>							
					</p:panel>		
					</h:form>		
				</p:fieldset>
				
			<br/>
			<p:fieldset legend="#{label['admin.logviewer.files.title']}" toggleable="false" collapsed="false">
				<h:form id="form">				
					<p:dataTable var="file" value="#{exampleLogViewer.logFileList}">  
				        <p:column headerText="#{label['admin.logviewer.files.name']}">  
				            <h:outputText value="#{file.name}" />  
				        </p:column>  
				        
				        <p:column headerText="#{label['admin.logviewer.files.size']}" style="text-align: center;">  
				            <h:outputText value="#{file.length() / 1024}">
				            	<f:convertNumber minFractionDigits="0" maxFractionDigits="0" groupingUsed="true" type="number"/>
				            </h:outputText>  
				            <h:outputText value="&nbsp;KB"/>
				        </p:column>  
				        
				        <p:column headerText="#{label['admin.logviewer.files.lastModified']}" style="text-align: center;"> 
				            <h:outputText value="#{file.lastModified()}">
				            	<f:convertDateTime pattern="dd-MM-yyyy HH:mm:ss z" timeZone="GMT-4" />
				            </h:outputText>  
				        </p:column>  
				  
				        <p:column headerText="#{label['admin.logviewer.files.actions']}" style="text-align: center;"> 			        
					        <p:commandButton icon="ui-icon-disk" value="#{label['admin.logviewer.files.download']}" 
										onclick="PrimeFaces.monitorDownload( showDownloadDialog, hideDownloadStop );"
										ajax="false" update="@all">
										<p:fileDownload value="#{exampleLogViewer.getFile(file.name)}"/>
							</p:commandButton>
						</p:column>			        
	    			</p:dataTable>
	    			<br/>
					<p:panel styleClass="no-border">
						<h:outputText value="#{label['admin.logviewer.files.maxlength']}" style="color:red;"/> : 
						<h:outputText value="#{exampleLogViewer.maxFileLength / 1024}" style="color:red;">
							<f:convertNumber minFractionDigits="0" maxFractionDigits="0" groupingUsed="true" type="number"/>
						</h:outputText> 
						<h:outputText value="&nbsp;KB" style="color:red;"/>
					</p:panel>
				</h:form>					
   			</p:fieldset>
				
			<br/>
		</p:panel>	
		<cc:LatamFooter/>
		
		<p:socket onMessage="handleLogMessage" channel="/applogger" autoConnect="true" />
		<script type="text/javascript">
			var MAX_LINES_ON_LOG = 50;
		    function handleLogMessage(data) {
		        var loggerContent = $(PrimeFaces.escapeClientId('viewer:applogger'));
		        
		        var textarea = loggerContent.get(0);
		        var total = ((textarea.value ? textarea.value : "") + data).split("\n");
		  		if (total.length > MAX_LINES_ON_LOG){ 
		  			total = total.slice(total.length - MAX_LINES_ON_LOG);
		  		}
		  		textarea.value = total.join("\n");		        
		        
		        //keep scroll  		        
		        $(loggerContent).scrollTop( textarea.scrollHeight );
		    }
		    function clearViewer(){
		        var loggerContent = $(PrimeFaces.escapeClientId('viewer:applogger'));
		        var textarea = loggerContent.get(0);
		        textarea.value = "";
		    }
		</script>
	</h:body>
</f:view>